package com.example.love_margin.Service.Impl;

import com.example.love_margin.Entity.Test;
import com.example.love_margin.Mapper.TestMapper;
import com.example.love_margin.Service.TestService;
import com.example.love_margin.Untils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.util.List;


@Service
public class TestServiceImpl implements TestService {
    @Autowired
    TestMapper testMapper;

    //全局变量，方便用来存储配置里的域名或者服务器ip
    @Value("${web.localUrl}")
    private String localUrl;

    //全局变量，方便更改图片本地存储目录
    @Value("${web.imgURL}")
    private String imgURL;

    @Override
    public Object addTest(Test t, MultipartFile file) {
        String msg = "";
        //获取上传图片的文件名带 / 的路径（非完全，后期查询接口对查询数据再拼接）存进对象t的Url的属性内
//        String fileName = file.getOriginalFilename();
//
//            t.setUrl(fileName);
            if(testMapper.addTest(t)==1){
//                if (FileUtils.upload(file, imgURL,fileName)){
//                    // 上传成功，给出页面提示
                msg = "添加成功";
                return ResultCode.SUCCESS.result(msg);
            }else {
                msg = "添加失败";
                return ResultCode.SUCCESS.result(msg);
            }
//        }else {
//            msg = "上传失败！";
//            return ResultCode.SUCCESS.result(msg);
//        }
    }

    @Override
    public Object select() {

        //给对象集合（List<Object>）里的对象的属性进行操作，获取或者更改再输出
        //下面是对List<img>里的所有img对象的Url属性的值进行获取和更改，然后输出
        //为了后期拼接图片所在服务器ip（或者网站的域名）的让其可以被访问而做的操作
        List<Test> ll = testMapper.select6();
//        for (int i = 0; i < ll.size(); i++) {
//            ll.get(i).setUrl(localUrl+ll.get(i).getUrl());
//            System.out.print(ll.get(i).getUrl());
//        }
        return ResultCode.SUCCESS.result(ll);
    }

    @Override
    public Object select1(Test y) {

        Test pa = new Test();
        if (y.getVarieties()!=null){
            pa.setVarieties(y.getVarieties());
        }
        if (y.getSpecies()!=null){
            pa.setSpecies(y.getSpecies());
        }

        if (y.getSex()!=null){
            pa.setSex(y.getSex());
        }
        System.out.println();
        System.out.println(pa);
        List<Test> ll = testMapper.selectManyKey(pa);
        System.out.println(ll);
        return ResultCode.SUCCESS.result(ll);
    }


}
